<?php
require "uuid.php";

function doPost($url, $post_data)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
	if($post_data != null)
	{
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($post_data)));
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
	}
	
	$curl_response = curl_exec($ch);
	curl_close($ch);
	return $curl_response;
}

function doPut($url, $post_data)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
	if($post_data != null)
	{
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($post_data)));
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
	}
	
	$curl_response = curl_exec($ch);
	curl_close($ch);
	return $curl_response;
}

function doDelete($url, $post_data)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
	if($post_data != null)
	{
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($post_data)));
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
	}
	
	$curl_response = curl_exec($ch);
	curl_close($ch);
	return $curl_response;
}

function doGet($url)
{
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_HTTPGET, true);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	$curl_response = curl_exec($curl);
	curl_close($curl);
	return $curl_response;
}

function createUUID()
{
    //$v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
	$v4uuid = UUID::v4();
	$v5uuid = UUID::v5($v4uuid, "RubberSoul");
	return $v5uuid;
}

function sendStatement($actor, $verb, $object, $context, $attachments = null)
{
	global $CFG;
	
	$statement = new Statement();
	$statement->setId(createUUID());
	$statement->setActor($actor);
	$statement->setVerb($verb);
	$statement->setObject($object);
	$statement->setObjectType("STATEMENT");
	$statement->setContext($context);
	
	if($attachments != null)
		$statement->setAttachments($attachments);
	
	$url = $CFG->endpoint."/statements?statementId=".$statement->getId();
	
	$post_data = json_encode($statement);

	return doPut($url, $post_data);
}

function getAgent($userId)
{
	global $DB;
	$user = $DB->get_record('user', array('id'=>$userId));
	$agent = new Agent();
	$agent->setId("moodle_user_".$userId);
	$agent->setMbox($user->email);
	$agent->setName($user->username);
	$agent->setObjectType("AGENT");
	return $agent;
}

function getGroup($courseid)
{
	global $DB;
	$course = get_course($courseid);
	$group = new Group();
	$group->setId("moodle_course_".$courseid);
	$group->setName($course->shortname);
	$group->setObjectType("GROUP");
	return $group;
}

function getVerb($verbName)
{
	$verb = new Verb();
	$verb->setId("moodle_verb_".$verbName);
	$verb->setName($verbName);
	return $verb;
}

function getElement($tableName, $elementId, $fromModule)
{
	global $DB;
	if($fromModule)
		$instance = $DB->get_field("course_modules", "instance", array("id"=>$elementId), MUST_EXIST);
	else $instance = $elementId;
	
	return $DB->get_record($tableName, array('id'=>$instance));
}
?>